package com.kefu.repository;

import com.kefu.entity.Message;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

public interface MessageRepository extends JpaRepository<Message, String> {
    // 根据会话ID查询消息
    List<Message> findBySessionIdOrderBySendTimeAsc(String sessionId);
    
    // 批量更新消息为已读
    @Transactional
    @Modifying
    @Query("update Message m set m.readStatus = 1 where m.sessionId = ?1 and m.senderType = 1 and m.readStatus = 0")
    int markAsReadBySessionId(String sessionId);
}
